home *** CD-ROM | disk | FTP | other *** search
- //-------------------------------------------------------------------------------------------
- // Affiliate module.
- //-------------------------------------------------------------------------------------------
-
- const Cc = Components.classes;
- const Ci = Components.interfaces;
-
- var EXPORTED_SYMBOLS = ["samfind_modaffiliate"];
-
- Components.utils.import("resource://samfind/samfind_modutils.jsm");
-
- var samfind_modaffiliate =
- {
- _base_urls : null,
- _styles : null,
- _latest_base_url : null,
- _latest_base_url_counter : 0,
-
- _init : function()
- {
- var file = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties).get("ProfD", Ci.nsIFile);
- file.append("samfindbaseurls.xml");
- if (file.exists())
- {
- var dom = samfind_modutils.loadXML(file);
- samfind_modaffiliate._base_urls = dom.getElementsByTagName("baseURL");
- samfind_modaffiliate.getStyles(dom);
- dom = null;
- }
- else
- {
- samfind_modutils._pref_branch.setCharPref("affiliates.sha1", "0");
- }
- file = null;
- //
- samfind_modaffiliate.fetchBaseURLs();
- },
-
- fetchBaseURLs : function()
- {
- try
- {
- var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
- xhr.open("POST", "http://samfind.com/XML/affiliates.php", true);
- xhr.channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
- xhr.setRequestHeader("Accept-Encoding", "gzip");
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- xhr.setRequestHeader("User-Agent", "samfind Toolbar");
- var timeout = samfind_modutils.setXhrAbortTimeout(xhr, 10000);
- xhr.onreadystatechange = function(e)
- {
- if (xhr.readyState == 4)
- {
- timeout.cancel();
- timeout = null;
- var retry = true;
- try
- {
- if (xhr.status == 200)
- {
- var dom_parser = Cc["@mozilla.org/xmlextras/domparser;1"].createInstance(Ci.nsIDOMParser);
- var dom = dom_parser.parseFromString(xhr.responseText, "text/xml");
- dom_parser = null;
- var document_element = dom.documentElement;
- if (document_element.nodeName != "parsererror")
- {
- retry = false;
- if (document_element.childNodes.length)
- {
- samfind_modutils._pref_branch.setCharPref("affiliates.sha1", xhr.getResponseHeader("X-samfind-sha1"));
- samfind_modutils.saveXML("samfindbaseurls.xml", dom);
- samfind_modaffiliate._base_urls = document_element.getElementsByTagName("baseURL");
- samfind_modaffiliate.getStyles(document_element);
- }
- }
- document_element = null;
- dom = null;
- }
- else if (xhr.status == 304)
- {
- retry = false;
- }
- }
- catch (e)
- {}
- xhr = null;
- if (retry)
- {
- samfind_modaffiliate.retryFetchBaseURLs();
- }
- }
- };
- xhr.send("sha1=" + samfind_modutils._pref_branch.getCharPref("affiliates.sha1"));
- }
- catch (e)
- {
- samfind_modaffiliate.retryFetchBaseURLs();
- }
- },
-
- retryFetchBaseURLs : function()
- {
- var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
- timer.initWithCallback({ notify : function() { samfind_modaffiliate.fetchBaseURLs(); } }, 30000, Ci.nsITimer.TYPE_ONE_SHOT);
- },
-
- getStyles : function(dom)
- {
- samfind_modaffiliate._styles = [];
- try
- {
- var styles = dom.getElementsByTagName("style");
- for (var i=0; i<styles.length; ++i)
- {
- var style = styles[i];
- var engine = style.getAttribute("engine");
- var value = style.getAttribute("value");
- if (engine != null && engine.length && value != null && value.length)
- {
- samfind_modaffiliate._styles[engine] = value;
- }
- }
- }
- catch (e)
- {}
- },
-
- checkAffiliateBaseURL : function(url)
- {
- if (!samfind_modaffiliate._base_urls)
- {
- return;
- }
- try
- {
- var url_object = samfind_modutils._io_service.newURI(url, null, null);
- var url_host = url_object.host;
- if (url_host.indexOf("www.") == 0)
- {
- url_host = url_host.substring(4);
- }
- var url_host_and_path = url_host + decodeURIComponent(url_object.path);
- for (var i=0; i<samfind_modaffiliate._base_urls.length; ++i)
- {
- var base_url = samfind_modaffiliate._base_urls[i].getAttribute("v");
- if (url_host_and_path.indexOf(base_url) != -1)
- {
- if (base_url == samfind_modaffiliate._latest_base_url)
- {
- if (++(samfind_modaffiliate._latest_base_url_counter) == 10)
- {
- samfind_modaffiliate._latest_base_url_counter = 0;
- samfind_modaffiliate.fetchAffiliateURL();
- }
- }
- else
- {
- samfind_modaffiliate._latest_base_url = base_url;
- samfind_modaffiliate._latest_base_url_counter = 0;
- samfind_modaffiliate.fetchAffiliateURL();
- }
- break;
- }
- }
- url_host_and_path = null;
- url_object = null;
- }
- catch (e)
- {}
- },
-
- fetchAffiliateURL : function()
- {
- try
- {
- var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
- xhr.open("POST", "http://samfind.com/XML/affiliates.php", true);
- xhr.channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- xhr.setRequestHeader("User-Agent", "samfind Toolbar");
- var timeout = samfind_modutils.setXhrAbortTimeout(xhr, 10000);
- xhr.onreadystatechange = function(e)
- {
- if (xhr.readyState == 4)
- {
- timeout.cancel();
- timeout = null;
- //
- try
- {
- if (xhr.status == 200)
- {
- samfind_modutils.loadIframe(xhr.responseText, null);
- }
- }
- catch (e)
- {}
- //
- xhr = null;
- }
- };
- xhr.send("baseURL=" + samfind_modaffiliate._latest_base_url);
- }
- catch (e)
- {}
- },
-
- //-------------------------------------------------------------------------------------------
- //
- //-------------------------------------------------------------------------------------------
-
- getElements : function(engine, doc, xpath)
- {
- var response = new Array();
- if (samfind_modaffiliate._base_urls && samfind_modaffiliate._styles != null && samfind_modaffiliate._styles[engine] != null)
- {
- var elements = doc.evaluate(xpath, doc, null, 0, null);
- var element = elements.iterateNext();
- while (element)
- {
- response.push(element);
- element = elements.iterateNext();
- }
- }
- return response;
- },
-
- elementIsAffiliate : function(url)
- {
- try
- {
- var url_object = samfind_modutils._io_service.newURI(url, null, null);
- var url_host = url_object.host;
- if (url_host.indexOf("www.") == 0)
- {
- url_host = url_host.substring(4);
- }
- var url_host_and_path = url_host + decodeURIComponent(url_object.path);
- for (var i=0; i<samfind_modaffiliate._base_urls.length; ++i)
- {
- var base_url = samfind_modaffiliate._base_urls[i];
- var base_url_value = base_url.getAttribute("v");
- if (url_host_and_path.indexOf(base_url_value) == 0)
- {
- return base_url;
- }
- }
- }
- catch (e)
- {}
- return null;
- },
-
- highlightElement : function(engine, element)
- {
- var style = samfind_modaffiliate._styles[engine];
- if (arguments.length == 2)
- {
- element.setAttribute("style", style);
- }
- else
- {
- var sub_elements = arguments[2];
- for (var j=0; j<sub_elements.length; ++j)
- {
- sub_elements[j].setAttribute("style", style);
- }
- }
- }
- };
-
- /**
- * Constructor.
- */
- (function() { this._init(); }).apply(samfind_modaffiliate);